package vn.njai.thousand_english_word.Activity;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.twotoasters.jazzylistview.JazzyListView;
import vn.njai.thousand_english_word.Entities.HistoryNodeItem;
import vn.njai.thousand_english_word.R;
import vn.njai.thousand_english_word.Utils.DataManager;

import java.util.ArrayList;

/**
 * Created by VUONG on 12/18/2014.
 */
public class ProgressDetailFragment extends Fragment implements MediaPlayer.OnCompletionListener{
    private String TAG = "ProgressDetailFragment";
    private static final String ARG_POSITION = "position";
    private static Context mContext;
    private Adapter mAdapter;
    private int mPosition;
    private View mRootView;
    public JazzyListView mListNode;
    public static ProgressDetailFragment newInstance(Context context,int position) {
        mContext = context;
        ProgressDetailFragment f = new ProgressDetailFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPosition = getArguments().getInt(ARG_POSITION);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_progress_detail, container, false);
        init();
        return mRootView;
    }

    public void init(){
        mAdapter = new Adapter();
        mListNode = (JazzyListView) mRootView.findViewById(R.id.listNodeProgress);
        mListNode.setAdapter(mAdapter);
        load();
        mListNode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ViewGroup viewGroup  = (ViewGroup) view.findViewById(R.id.viewShow);
                if(viewGroup.getVisibility() == View.GONE) {
                    viewGroup.setVisibility(View.VISIBLE);
                    mAdapter.getItem(i).mIsShow = true;
                }else{
                    viewGroup.setVisibility(View.GONE);
                    mAdapter.getItem(i).mIsShow = false;
                }
            }
        });
        mListNode.setItemsCanFocus(true);
    }

    public void load(){
        ArrayList<HistoryNodeItem> nodeItems = new ArrayList<HistoryNodeItem>();
        switch (mPosition){
            case 0:
                mAdapter.addAll(DataManager.getInstance().mDetailData.mListLeaned);
                break;
            case 1:
                mAdapter.addAll(DataManager.getInstance().mDetailData.mListTwoTime);
                break;
            case 2:
                mAdapter.addAll(DataManager.getInstance().mDetailData.mListOneTime);
                break;
            case 3:
                mAdapter.addAll(DataManager.getInstance().mDetailData.mListNever);
                break;
        }


    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        if(mediaPlayer!=null)
            mediaPlayer.release();
    }

    public class Adapter extends ArrayAdapter<HistoryNodeItem> {
        public class ViewHolder{
            @InjectView(R.id.txtWord) public TextView word;
            @InjectView(R.id.txtRank) public TextView rank;
            @InjectView(R.id.txtPhonetic) public TextView phonetic;
            @InjectView(R.id.txtMeans) public TextView mean;
            @InjectView(R.id.viewShow) public View showView;
            @InjectView(R.id.viewAll) public View allView;
            @InjectView(R.id.btnSound) public ImageButton btnSound;
            @InjectView(R.id.txtType) public TextView txtType;
            public ViewHolder(View view){
                ButterKnife.inject(this, view);
            }
        }

        public Adapter() {
            super(mContext, 0, new ArrayList<HistoryNodeItem>());
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            View view = convertView;
            final ViewHolder viewHolder;
            final HistoryNodeItem item = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (view == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.custom_node_history, parent, false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.word.setText(item.mWord);
            viewHolder.rank.setText(item.mRank + "");
            viewHolder.phonetic.setText(item.mPhonetic);
            viewHolder.mean.setText(item.mMeans);
            viewHolder.txtType.setText(item.mType);
            if(item.mIsShow == true){
                viewHolder.showView.setVisibility(View.VISIBLE);
            }else{
                viewHolder.showView.setVisibility(View.GONE);
            }
            viewHolder.allView.setBackgroundColor(item.mColor);
            viewHolder.btnSound.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Handler handler = new Handler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                MediaPlayer mediaPlayer = new MediaPlayer();
                                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                String url = "http://soundoftext.com/audio/English/" + item.mWord.toLowerCase() + ".mp3";
                                Log.e(TAG, url);
                                DataManager.getInstance().loadVolume();
                                mediaPlayer.setVolume((float)DataManager.getInstance().mVolume/100,(float)DataManager.getInstance().mVolume/100);
                                mediaPlayer.setDataSource(url);
                                mediaPlayer.prepare(); // might take long! (for buffering, etc)
                                mediaPlayer.start();
                            }catch (Exception ex){

                            }
                        }
                    });
                }
            });
            return view;
        }
    }
}
